package com.imay.live.view;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.imay.live.R;
import com.imay.live.manager.FamilyAccountManager;
import com.imay.live.utils.GlideUtil;
import com.imay.live.utils.ViewUtil;

import java.util.List;

/**
 * Created by Administrator on 2017/8/31 0031.
 */

public class FamilyItemView extends RelativeLayout implements View.OnClickListener {

    TextView tv_item_title;
    TextView tv_item_count;
    TextView tv_item_name;
    RecyclerView rl_item;
    RelativeLayout item_1, item_2, item_3, item_4, item_5;
    LinearLayout ll_family;

    ImageView iv_item;

    private OnItemClick onItemClick;
    private OnNodataClick onNodataClick;
    View tv_family_live_in, tv_family_live_nodata, tv_family_ranking_nodata;

    public OnItemClick getOnItemClick() {
        return onItemClick;
    }

    public void setOnItemClick(OnItemClick onItemClick) {
        this.onItemClick = onItemClick;
    }

    public OnNodataClick getOnNodataClick() {
        return onNodataClick;
    }

    public void setOnNodataClick(OnNodataClick onNodataClick) {
        this.onNodataClick = onNodataClick;
    }

    @Override
    public void onClick(View v) {
        int position = -1;
        switch (v.getId()) {
            case R.id.item_1:
                position = 0;
                break;
            case R.id.item_2:
                position = 1;
                break;
            case R.id.item_3:
                position = 2;
                break;
            case R.id.item_4:
                position = 3;
                break;
            case R.id.item_5:
                position = 4;
                break;
            case R.id.tv_family_live_in:
                if (onNodataClick != null) {
                    onNodataClick.noDataClick();
                }
                break;
        }
        if (onItemClick != null) {
            onItemClick.itemClick(position);
        }
    }

    public interface OnItemClick {
        void itemClick(int position);
    }

    public interface OnNodataClick {
        void noDataClick();
    }

    public FamilyItemView(Context context) {
        this(context, null);
    }

    public FamilyItemView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public FamilyItemView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        LayoutInflater.from(context).inflate(R.layout.view_family_item, this, true);

        init();
    }

    private void init() {
        tv_item_title = ViewUtil.findView(this, R.id.tv_item_title);
        tv_item_count = ViewUtil.findView(this, R.id.tv_item_count);
        tv_item_name = ViewUtil.findView(this, R.id.tv_item_name);
        iv_item = ViewUtil.findView(this, R.id.iv_item);
        ll_family = ViewUtil.findView(this, R.id.ll_family);
        tv_family_live_in = ViewUtil.findView(this, R.id.tv_family_live_in);
        tv_family_live_nodata = ViewUtil.findView(this, R.id.tv_family_live_nodata);
        tv_family_ranking_nodata = ViewUtil.findView(this, R.id.tv_family_ranking_nodata);

        item_1 = ViewUtil.findView(this, R.id.item_1);
        item_2 = ViewUtil.findView(this, R.id.item_2);
        item_3 = ViewUtil.findView(this, R.id.item_3);
        item_4 = ViewUtil.findView(this, R.id.item_4);
        item_5 = ViewUtil.findView(this, R.id.item_5);

        item_1.setOnClickListener(this);
        item_2.setOnClickListener(this);
        item_3.setOnClickListener(this);
        item_4.setOnClickListener(this);
        item_5.setOnClickListener(this);

        tv_family_live_in.setOnClickListener(this);
    }

    public void setTitle(String c) {
        tv_item_title.setText(c);
        tv_item_title.setVisibility(VISIBLE);
    }

    public void setTitle(int rid) {
        tv_item_title.setText(rid);
        tv_item_title.setVisibility(VISIBLE);
    }

    public void setName(String c) {
        tv_item_name.setText(c);
        tv_item_name.setVisibility(VISIBLE);
    }

    public void setName(int rid) {
        tv_item_name.setText(rid);
        tv_item_name.setVisibility(VISIBLE);
    }

    public void setLiveCount(int liveCount) {
        if (liveCount > 0) {
            tv_item_count.setText(getResources().getString(R.string.str_family_live_count, liveCount));
            tv_item_count.setVisibility(VISIBLE);
        } else {
            tv_item_count.setVisibility(INVISIBLE);
        }
    }

    public void setUserCount(int liveCount) {
        if (liveCount > 0) {
            tv_item_count.setText(getResources().getString(R.string.str_family_users_count, liveCount));
            tv_item_count.setVisibility(VISIBLE);
        } else {
            tv_item_count.setVisibility(INVISIBLE);
        }
    }

    public void setImageHidden() {
        iv_item.setVisibility(INVISIBLE);
        tv_family_live_in.setVisibility(GONE);
    }

    public void setImageShow() {
        iv_item.setVisibility(VISIBLE);
        tv_family_live_in.setVisibility(GONE);
    }

    public RecyclerView getRecyclerView() {
        return rl_item;
    }

    public void hiddenImageView() {
        ll_family.setVisibility(VISIBLE);

        item_1.setVisibility(GONE);
        item_2.setVisibility(GONE);
        item_3.setVisibility(GONE);
        item_4.setVisibility(GONE);
        item_5.setVisibility(GONE);
    }

    public void setNewData(List<String> datas, boolean isLive) {
        ll_family.setVisibility(VISIBLE);
        tv_family_live_nodata.setVisibility(GONE);
        if (datas == null || datas.isEmpty()) {
            item_1.setVisibility(GONE);
            item_2.setVisibility(GONE);
            item_3.setVisibility(GONE);
            item_4.setVisibility(GONE);
            item_5.setVisibility(GONE);
            if (isLive) {
                tv_family_live_nodata.setVisibility(VISIBLE);
            }
            return;
        } else {
            if (datas.size() > 4) {
                item_1.setVisibility(VISIBLE);
                item_2.setVisibility(VISIBLE);
                item_3.setVisibility(VISIBLE);
                item_4.setVisibility(VISIBLE);
                item_5.setVisibility(VISIBLE);
            } else if (datas.size() > 3) {
                item_1.setVisibility(VISIBLE);
                item_2.setVisibility(VISIBLE);
                item_3.setVisibility(VISIBLE);
                item_4.setVisibility(VISIBLE);
                item_5.setVisibility(GONE);
            } else if (datas.size() > 2) {
                item_1.setVisibility(VISIBLE);
                item_2.setVisibility(VISIBLE);
                item_3.setVisibility(VISIBLE);
                item_4.setVisibility(GONE);
                item_5.setVisibility(GONE);
            } else if (datas.size() > 1) {
                item_1.setVisibility(VISIBLE);
                item_2.setVisibility(VISIBLE);
                item_3.setVisibility(GONE);
                item_4.setVisibility(GONE);
                item_5.setVisibility(GONE);
            } else if (datas.size() > 0) {
                item_1.setVisibility(VISIBLE);
                item_2.setVisibility(GONE);
                item_3.setVisibility(GONE);
                item_4.setVisibility(GONE);
                item_5.setVisibility(GONE);
            }
            for (int i = 0; i < datas.size(); i++) {
                View view = getView(i);
                if (view != null) {
                    ImageView simpleDraweeView = ViewUtil.findView(view, R.id.civ_item_family_home_img);
                    String image = datas.get(i);
                    GlideUtil.loadCircleImage(simpleDraweeView, image, R.mipmap.placeholder);
                }
            }
        }
    }

    public void setRank(List<Integer> ranks) {
        tv_family_ranking_nodata.setVisibility(GONE);
        if (ranks != null && !ranks.isEmpty()) {
            for (int i = 0; i < ranks.size(); i++) {
                View view = getView(i);
                if (view != null) {
                    TextView tv_item_family_rank = ViewUtil.findView(view, R.id.tv_item_family_rank);
                    TextView tv_item_family_home = ViewUtil.findView(view, R.id.tv_item_family_home);
                    int rank = ranks.get(i);
                    tv_item_family_rank.setVisibility(VISIBLE);
                    tv_item_family_home.setVisibility(GONE);
                    int rid = R.drawable.shape_oval_f4;
                    if (rank == 0) {
                        rid = R.drawable.shape_oval_f4;
                    } else if (rank == 1) {
                        rid = R.drawable.shape_oval_d5;
                    } else if (rank == 2) {
                        rid = R.drawable.shape_oval_d9;
                    }
                    tv_item_family_rank.setBackgroundResource(rid);
                    tv_item_family_rank.setText(String.valueOf(rank + 1));
                }
            }
        } else {
            tv_family_ranking_nodata.setVisibility(VISIBLE);
        }
    }

    private View getView(int i) {
        View view = null;
        switch (i) {
            case 0:
                view = item_1;
                break;
            case 1:
                view = item_2;
                break;
            case 2:
                view = item_3;
                break;
            case 3:
                view = item_4;
                break;
            case 4:
                view = item_5;
                break;
        }
        return view;
    }

    public void setAdmin(List<Integer> admin) {
        if (admin != null && !admin.isEmpty()) {
            for (int i = 0; i < admin.size(); i++) {
                View view = getView(i);
                if (view != null) {
                    TextView tv_item_family_home = ViewUtil.findView(view, R.id.tv_item_family_home);
                    TextView tv_item_family_rank = ViewUtil.findView(view, R.id.tv_item_family_rank);
                    tv_item_family_rank.setVisibility(GONE);
                    int familyAdmin = admin.get(i);
                    if (familyAdmin > 0) {
                        tv_item_family_home.setText(FamilyAccountManager.getAdminRstrId(familyAdmin));
                        tv_item_family_home.setVisibility(VISIBLE);
                    } else {
                        tv_item_family_home.setVisibility(GONE);
                    }
                }
            }
        }
    }

    public void setViewHidden() {
        hidden(item_1);
        hidden(item_2);
        hidden(item_3);
        hidden(item_4);
        hidden(item_5);
    }

    private void hidden(View view) {
        TextView tv_item_family_home = ViewUtil.findView(view, R.id.tv_item_family_home);
        tv_item_family_home.setVisibility(INVISIBLE);
        TextView tv_item_family_rank = ViewUtil.findView(view, R.id.tv_item_family_rank);
        tv_item_family_rank.setVisibility(INVISIBLE);
    }
}
